package net.danielthompson.danray.structures;

/**
 * User: daniel
 * Date: 7/21/13
 * Time: 15:44
 */
public class BoundingBox implements Boundable {
   private Point _point1;
   private Point _point2;

   private Boundable _leftChild;
   private Boundable _rightChild;

   public BoundingBox (Point point1, Point point2) {
      _point1 = point1;
      _point2 = point2;
   }

   public double GetVolume() {
      return Math.abs((_point2.getX() - _point1.getX()) *   (_point2.getY() - _point1.getY()) * (_point2.getZ() - _point1.getZ()));
   }

   @Override
   public BoundingBox GetBoundingBox() {
      return this;
   }

   public void Translate(Point p) {
      _point1.Plus(p);
      _point2.Plus(p);
   }

   public boolean equals(Object obj) {
      if (obj == null)
         return false;
      if (obj == this)
         return true;
      if (!(obj instanceof BoundingBox))
         return false;

      BoundingBox rhs = (BoundingBox) obj;
      return (_point1.equals(rhs._point1) && _point2.equals(rhs._point2));
   }

   public static BoundingBox GetBoundingBox(BoundingBox box1, BoundingBox box2) {
      double p1x = Math.min(box1._point1.getX(), box2._point1.getX());
      double p1y = Math.min(box1._point1.getY(), box2._point1.getY());
      double p1z = Math.min(box1._point1.getZ(), box2._point1.getZ());
      Point p1 = new Point(p1x, p1y, p1z);

      double p2x = Math.max(box1._point1.getX(), box2._point1.getX());
      double p2y = Math.max(box1._point1.getY(), box2._point1.getY());
      double p2z = Math.max(box1._point1.getZ(), box2._point1.getZ());
      Point p2 = new Point(p2x, p2y, p2z);

      BoundingBox box = new BoundingBox(p1, p2);
      return box;
   }

   public Boundable getLeftChild() {
      return _leftChild;
   }

   public void setLeftChild(Boundable leftChild) {
      _leftChild = leftChild;
   }

   public Boundable getRightChild() {
      return _rightChild;
   }

   public void setRightChild(Boundable rightChild) {
      _rightChild = rightChild;
   }
}
